package com.itheima;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 1.构造函数
 * ---1.ArrayList()
 *
 * 2.成员方法
 * ---1.add(Object obj)
 * ---2.add(int index,Object obj)
 * ---3.remove(int index)
 * ---4.get(int index)
 * ---5.set(int index,Object obj)
 * ---6.size()
 *
 */
public class ArrayListDemo {

    public static void main (String[] args){
        //1. 创建ArrayList对象
        ArrayList<String> list = new ArrayList<String>();
        //2. 往ArrayList集合中添加数据
        list.add("hello");
        list.add("world");
        list.add("java");
        //3. 往ArrayList集合中添加数据，指定索引
        list.add(1,"javaSE");
        //4. 获取ArrayList集合中指定索引的数据
        String s = list.get(0);
        System.out.println("获取的数据为："+s);
        //5. 修改ArrayList集合中指定索引的数据
        list.set(1,"222");
        //6. 删除ArrayList集合中指定索引的数据
        list.remove(0);
        System.out.println(list);
        list.add("zhang");

        System.out.println("----for循环遍历--------------------------------------------------------");

        //1. for循环遍历ArrayList集合
        for (int i = 0; i < list.size(); i++) {
            String obj = list.get(i);
            System.out.println(obj);
        }
        System.out.println("----增强for循环遍历------");
        //2. 增强for循环遍历ArrayList集合
        for (String obj : list) {
            System.out.println(obj);
        }
        System.out.println("----迭代器遍历------");
        //3. 迭代器遍历ArrayList集合
        Iterator<String> iterator = list.iterator();

        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        System.out.println("----Lambda表达式------");
        //4. Lambda表达式
        list.forEach(System.out::println);
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        list.forEach(name-> System.out.println(name));

    }

}